package com.zzz.schedule.controller;

import com.zzz.schedule.common.Result;
import com.zzz.schedule.pojo.SysSchedule;
import com.zzz.schedule.service.SysScheduleService;
import com.zzz.schedule.service.SysUserService;
import com.zzz.schedule.service.impl.SysScheduleServiceImpl;
import com.zzz.schedule.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @author zzr
 * @date: 2025/07/14  19:14
 * @description:
 * 增加日程的请求 /schedule/add
 * 删除日程的请求 /schedule/remove
 * 修改日程的请求 /schedule/update
 * 查询日程的请求 /schedule/find
 */

@WebServlet("/schedule/*")
public class SysScheduleController extends BaseController {

    private SysScheduleService scheduleService = new SysScheduleServiceImpl();
    protected void findAllSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收请求中的 uid 参数
        int uid = Integer.parseInt(req.getParameter("uid"));

        // 查询用户的所有日程
        List<SysSchedule> itemList = scheduleService.finItemListByUid(uid);

        // 将用户的所有日程放入一个 Result 对象
        Map data = new HashMap();
        data.put("itemList", itemList);
        Result result = Result.ok(data);
        // 将Result 对象转换为 JSON 相应给客户端
        WebUtil.writeJson(resp, result);
    }

    /**
     * 为当前用户增加一个新的空的日程记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addDefaultSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收请求中的 uid 参数
        int uid = Integer.parseInt(req.getParameter("uid"));
        // 调用服务处方法，向数据库中增加一条空记录
        scheduleService.addDefault(uid);

        WebUtil.writeJson(resp,Result.ok(null));
    }

    /**
     * 根据 uid 进行数据更新
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收请求体中的JSON 字符串 转换成一个 SysSchedule
        SysSchedule schedule = WebUtil.readJson(req, SysSchedule.class);
        // 调用服务层方法，将信息更新进入数据库
        scheduleService.updateSchedule(schedule);

        WebUtil.writeJson(resp,Result.ok(null));
    }

    /**
     * 删除 sid 的日程
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void removeSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int sid = Integer.parseInt(req.getParameter("sid"));
        scheduleService.removeSchedule(sid);
        WebUtil.writeJson(resp,Result.ok(null));
    }
}
